from django.db import models
from base import *

__all__ = ["ObjectType", "ChildObject", "Field", "ValidValue", "Property", "PropertyGroup", "PropertyGroupMember"]

class ObjectType(MetadataObject):
    _collection_sets = []
    name = models.CharField(max_length=256, blank=True)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.name

class ChildObject(MetadataObject):
    _collection_sets = []
    objecttype = models.ForeignKey(ObjectType)
    child_objecttype = models.ForeignKey(ObjectType, related_name='child_objecttype')
    name = models.CharField(max_length=256)
    cardinality = models.CharField(max_length=256, choices=CARDINALITY, default='0..1')
    _container_field = "objecttype_id"
    ObjectType._collection_sets.append('child_objecttype')

class Field(MetadataObject):
    _collection_sets = []
    name = models.CharField(max_length=256, blank=True)
    description = models.TextField(blank=True)
    datatype = models.CharField(max_length=256, choices=DATA_TYPES, default='string')
    cardinality = models.CharField(max_length=256, choices=CARDINALITY, default='0..1')
    isEnumerated = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

class ValidValue(MetadataObject):
    _collection_sets = []
    field = models.ForeignKey(Field)    
    value = models.CharField(max_length=1024, blank=True)
    description = models.TextField(blank=True)
    label = models.CharField(max_length=1024, blank=True)
    _container_field = "field_id"
    Field._collection_sets.append('validvalue_set')

class Property(Field):
    _collection_sets = []
    objecttype = models.ForeignKey(ObjectType)
    ucd = models.CharField(max_length=1024, blank=True)
    _container_field = "objecttype_id"
    ObjectType._collection_sets.append('property_set')

class PropertyGroup(MetadataObject):
    _collection_sets = []
    objecttype = models.ForeignKey(ObjectType)
    name = models.CharField(max_length=256)
    description = models.TextField(blank=True)
    _container_field = "objecttype_id"
    ObjectType._collection_sets.append('propertygroup_set')

class PropertyGroupMember(MetadataObject):
    _collection_sets = []
    propertygroup = models.ForeignKey(PropertyGroup)
    property = models.ForeignKey(Property)
    _container_field = "propertygroup_id"
    PropertyGroup._collection_sets.append('propertygroupmember_set')
